<template>
  <div id="visitor">
    <Loading v-if="total == null"></Loading>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">访客管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>访客列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 搜索访客区域 -->
      <el-row>
        <el-col :span="8"
          ><el-input
            placeholder="请输入访客昵称关键字查询"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            @clear="getVisitorList"
          >
            <template #append>
              <el-button
                icon="el-icon-search"
                @click="getVisitorList"
              ></el-button>
            </template> </el-input
        ></el-col>
      </el-row>

      <!-- 访客列表 -->
      <el-table :data="visitorList" border stripe class="mt10">
        <el-table-column type="index" align="center"></el-table-column>
        <el-table-column
          label="昵称"
          prop="visitorName"
          align="center"
        ></el-table-column>
        <el-table-column label="头像" width="70px" align="center">
          <template #default="scope">
            <div class="img-wrapper">
              <img :src="scope.row.avatarUrl" alt="无" />
            </div> </template
        ></el-table-column>
        <el-table-column label="邮箱" prop="email" align="center">
        </el-table-column>
        <el-table-column label="创建时间" width="190px" align="center">
          <template #default="scope">
            {{ formatTime(scope.row.createdAt) }}
          </template></el-table-column
        >
        <el-table-column label="最后活跃" width="190px" align="center">
          <template #default="scope">
            {{ formatTime(scope.row.lastActive) }}
          </template></el-table-column
        >
        <el-table-column label="更多信息" width="80px" align="center">
          <template #default="scope">
            <el-button
              type="info"
              icon="el-icon-info"
              size="mini"
              @click="getMore(scope.row._id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[10, 20, 50]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 访客评论列表对话框 -->
    <el-dialog v-model="isShowMoreDialog" width="700px">
      <el-table :data="comments">
        <el-table-column label="创建时间" width="190px" align="center">
          <template #default="scope">
            {{ formatTime(scope.row.createdAt) }}
          </template></el-table-column
        >
        <el-table-column
          property="object.title"
          label="文章标题"
          width="220"
        ></el-table-column>
        <el-table-column property="content" label="评论内容"></el-table-column>
        <el-table-column label="操作" width="60px" align="center">
          <template #default="scope">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteComment(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from "@vue/reactivity";
import { formatTime } from "../../libs/utils";
import { getData, deleteData } from "../../services/http";
import { getCurrentInstance, onBeforeMount } from "@vue/runtime-core";
export default {
  name: "Visitor",
  setup() {
    const { proxy } = getCurrentInstance();
    const state = reactive({
      visitorList: [],
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 10,
      },
      total: null,
      comments: [],
    });
    let visitorId = null;
    const isShowMoreDialog = ref(false);
    const stateAsRefs = toRefs(state);
    const getVisitorList = async () => {
      const { data: res } = await getData("visitor/list", {
        params: state.queryInfo,
      });
      state.visitorList = res.visitors;
      state.total = res.total;
    };
    const getVisitorDetail = async (id) => {
      const { data: res } = await getData(`visitor/${id}`);
      state.comments = res.comments;
    };
    const getMore = (id) => {
      visitorId = id;
      isShowMoreDialog.value = true;
      getVisitorDetail(id);
    };
    const deleteComment = async (row) => {
      const { content, _id } = row;
      proxy
        .$confirm(`此操作将永久删除评论 ${content}, 是否继续?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(async () => {
          await deleteData(`rest/comment/${_id}`, `已成功删除评论 ${content}`);
          getMore(visitorId);
        })
        .catch(() => {
          proxy.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    };
    const handleCurrentChange = (newPage) => {
      state.queryInfo.pageNum = newPage;
      getVisitorList();
    };
    const handleSizeChange = (newSize) => {
      state.queryInfo.pageSize = newSize;
      getVisitorList();
    };
    onBeforeMount(() => {
      getVisitorList();
    });
    return {
      ...stateAsRefs,
      isShowMoreDialog,
      handleCurrentChange,
      handleSizeChange,
      getVisitorList,
      formatTime,
      getMore,
      deleteComment,
    };
  },
};
</script>

<style>
.img-wrapper img {
  width: 32px;
  height: 32px;
  object-fit: cover;
}
</style>